<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="房间编号" prop="roomcode">
      <el-input v-model="dataForm.roomcode" placeholder="房间编号" disabled="true"></el-input>
    </el-form-item>
       <el-form-item label="房间名称" prop="roomname">
      <el-input v-model="dataForm.roomname" placeholder="房间名称" disabled="true"></el-input>
    </el-form-item>
    <el-form-item label="用户名" prop="username">
      <el-input v-model="dataForm.username" placeholder="用户id" disabled="true"></el-input>
    </el-form-item>
    <el-form-item label="绑定时间" prop="createtime">
      <el-input v-model="dataForm.createtime" placeholder="绑定时间" disabled="true"></el-input>
    </el-form-item>
    <el-form-item label="入住状态" prop="status">
      <el-radio-group v-model="dataForm.status" size="big">
          <el-radio :label="0">预定</el-radio>
          <el-radio :label="1">已入住</el-radio>
        </el-radio-group>
    </el-form-item>
    <el-form-item label="入住次数" prop="settlenum">
      <el-input v-model="dataForm.settlenum" placeholder="入住次数"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data: function () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          roomcode: 'sdadas',
          username: '',
          roomname: '',
          roomid: '',
          userid: '',
          status: 0,
          settlenum: ''
        },
        dataRule: {
          roomid: [
            {required: true, message: '房间id不能为空', trigger: 'blur'}
          ],
          userid: [
            {required: true, message: '用户id不能为空', trigger: 'blur'}
          ],
          createtime: [
            {required: true, message: '绑定时间不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/roomuser/roomuser/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.roomid = data.roomuser.roomid
                this.dataForm.userid = data.roomuser.userid
                this.dataForm.createtime = data.roomuser.createtime
                this.dataForm.status = data.roomuser.status
                this.dataForm.settlenum = data.roomuser.settlenum
                this.dataForm.username = data.roomuser.username
                this.dataForm.roomname = data.roomuser.roomname
                this.dataForm.roomcode = data.roomuser.roomcode
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/roomuser/roomuser/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'roomid': this.dataForm.roomid,
                'userid': this.dataForm.userid,
                'createtime': this.dataForm.createtime,
                'status': this.dataForm.status,
                'settlenum': this.dataForm.settlenum,
                'username': this.dataForm.username,
                'roomcode': this.dataForm.roomcode,
                'roomname': this.dataForm.roomname
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1000,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
